﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using ESRI.ArcGIS.Geodatabase;
using ESRI.ArcGIS.Geometry;
using ESRI.ArcGIS.esriSystem;
using ESRI.ArcGIS.DataSourcesGDB;
using ESRI.ArcGIS.Carto;
using ESRI.ArcGIS.Display;
using System.Threading;

namespace Main
{
    public class PipeShapping
    {
        private DataTable _ptDt = null;
        private DataTable _lineDt = null;
        IGraphicsContainer gc;

        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="ptdt"></param>
        /// <param name="linedt"></param>
        public PipeShapping(DataTable ptdt, DataTable linedt)
        {
            this._ptDt = ptdt;
            this._lineDt = linedt;
            IGraphicsLayer graphiclayer = AddSubGraphicsLayer(Extern.axMapControl地图预览.Map, "mayGraphicLayer");
            gc = graphiclayer as IGraphicsContainer;
            //删除此graphicslayer中的所有元素
            gc.DeleteAllElements();
            //刷新
            Extern.axMapControl地图预览.ActiveView.PartialRefresh(esriViewDrawPhase.esriViewGraphics, null, null);
        }

        /// <summary>
        /// 数据成图
        /// </summary>
        public void Shapping()
        {
            ClearWorckSpace();
            DrawShape();
        }

        private void DrawShape()
        {
            DrawPoint();
            DrawLine();
        }

        /// <summary>
        /// 绘制管点
        /// </summary>
        private void DrawPoint()
        {
            List<string> layersList = new List<string>();
            foreach (DataRow i in this._ptDt.Rows)
                if (!layersList.Contains((string)i["GXZL"])) layersList.Add((string)i["GXZL"]);
            IWorkspace pWSF = FindWsByDefault();
            IEnumDataset dataSet = pWSF.get_Datasets(esriDatasetType.esriDTFeatureDataset);
            IFeatureDataset data = dataSet.Next() as IFeatureDataset;
            while (data != null)
            {
                if (data.Name == "LSGXPT.WorkSpaceForLSPipe")
                    break;
                else data = dataSet.Next() as IFeatureDataset;
            }

            #region//属性编辑
            IFields pFields = new FieldsClass();
            IFieldsEdit pFieldsEdit = (IFieldsEdit)pFields;


            IField pField = new FieldClass();
            IFieldEdit pFieldEdit = (IFieldEdit)pField;
            pFieldEdit.Name_2 = "Shape";
            pFieldEdit.Type_2 = esriFieldType.esriFieldTypeGeometry;
            IGeometryDef pGeometryDef = new GeometryDefClass();
            IGeometryDefEdit pGDefEdit = (IGeometryDefEdit)pGeometryDef;
            pGDefEdit.GeometryType_2 = esriGeometryType.esriGeometryPoint;

            pFieldEdit.GeometryDef_2 = pGeometryDef;
            pGDefEdit.SpatialReference_2 = LoadProjectedCoordinateSystem();
            pFieldsEdit.AddField(pField);

            pField = new FieldClass();
            pFieldEdit = (IFieldEdit)pField;
            pFieldEdit.Length_2 = 20;
            pFieldEdit.Name_2 = "GXZL";
            pFieldEdit.AliasName_2 = "管线种类";
            pFieldEdit.Type_2 = esriFieldType.esriFieldTypeString;
            pFieldsEdit.AddField(pField);

            pField = new FieldClass();
            pFieldEdit = (IFieldEdit)pField;
            pFieldEdit.Length_2 = 10;
            pFieldEdit.Name_2 = "QSDW";
            pFieldEdit.AliasName_2 = "权属单位";
            pFieldEdit.Type_2 = esriFieldType.esriFieldTypeString;
            pFieldsEdit.AddField(pField);

            pField = new FieldClass();
            pFieldEdit = (IFieldEdit)pField;
            pFieldEdit.Length_2 = 10;
            pFieldEdit.Name_2 = "GDMS";
            pFieldEdit.AliasName_2 = "管点埋深";
            pFieldEdit.Type_2 = esriFieldType.esriFieldTypeDouble;
            pFieldsEdit.AddField(pField);

            pField = new FieldClass();
            pFieldEdit = (IFieldEdit)pField;
            pFieldEdit.Length_2 = 10;
            pFieldEdit.Name_2 = "TZD";
            pFieldEdit.AliasName_2 = "特征点";
            pFieldEdit.Type_2 = esriFieldType.esriFieldTypeString;
            pFieldsEdit.AddField(pField);

            pField = new FieldClass();
            pFieldEdit = (IFieldEdit)pField;
            pFieldEdit.Length_2 = 10;
            pFieldEdit.Name_2 = "FSW";
            pFieldEdit.AliasName_2 = "附属物";
            pFieldEdit.Type_2 = esriFieldType.esriFieldTypeString;
            pFieldsEdit.AddField(pField);

            pField = new FieldClass();
            pFieldEdit = (IFieldEdit)pField;
            pFieldEdit.Length_2 = 10;
            pFieldEdit.Name_2 = "GC";
            pFieldEdit.AliasName_2 = "高程";
            pFieldEdit.Type_2 = esriFieldType.esriFieldTypeDouble;
            pFieldsEdit.AddField(pField);

            pField = new FieldClass();
            pFieldEdit = (IFieldEdit)pField;
            pFieldEdit.Length_2 = 10;
            pFieldEdit.Name_2 = "JSND";
            pFieldEdit.AliasName_2 = "建设年代";
            pFieldEdit.Type_2 = esriFieldType.esriFieldTypeDate;
            pFieldsEdit.AddField(pField);

            pField = new FieldClass();
            pFieldEdit = (IFieldEdit)pField;
            pFieldEdit.Length_2 = 10;
            pFieldEdit.Name_2 = "DCRQ";
            pFieldEdit.AliasName_2 = "调查日期";
            pFieldEdit.Type_2 = esriFieldType.esriFieldTypeDate;
            pFieldsEdit.AddField(pField);

            pField = new FieldClass();
            pFieldEdit = (IFieldEdit)pField;
            pFieldEdit.Length_2 = 10;
            pFieldEdit.Name_2 = "DCDW";
            pFieldEdit.AliasName_2 = "调查单位";
            pFieldEdit.Type_2 = esriFieldType.esriFieldTypeString;
            pFieldsEdit.AddField(pField);

            pField = new FieldClass();
            pFieldEdit = (IFieldEdit)pField;
            pFieldEdit.Length_2 = 10;
            pFieldEdit.Name_2 = "SSDL";
            pFieldEdit.AliasName_2 = "所属道路";
            pFieldEdit.Type_2 = esriFieldType.esriFieldTypeString;
            pFieldsEdit.AddField(pField);

            pField = new FieldClass();
            pFieldEdit = (IFieldEdit)pField;
            pFieldEdit.Length_2 = 10;
            pFieldEdit.Name_2 = "TFH";
            pFieldEdit.AliasName_2 = "图幅号";
            pFieldEdit.Type_2 = esriFieldType.esriFieldTypeString;
            pFieldsEdit.AddField(pField);

            pField = new FieldClass();
            pFieldEdit = (IFieldEdit)pField;
            pFieldEdit.Length_2 = 10;
            pFieldEdit.Name_2 = "WTDH";
            pFieldEdit.AliasName_2 = "物探点号";
            pFieldEdit.Type_2 = esriFieldType.esriFieldTypeString;
            pFieldsEdit.AddField(pField);

            pField = new FieldClass();
            pFieldEdit = (IFieldEdit)pField;
            pFieldEdit.Length_2 = 10;
            pFieldEdit.Name_2 = "TSDH";
            pFieldEdit.AliasName_2 = "图上点号";
            pFieldEdit.Type_2 = esriFieldType.esriFieldTypeString;
            pFieldsEdit.AddField(pField);

            pField = new FieldClass();
            pFieldEdit = (IFieldEdit)pField;
            pFieldEdit.Length_2 = 10;
            pFieldEdit.Name_2 = "XZJ";
            pFieldEdit.AliasName_2 = "旋转角";
            pFieldEdit.Type_2 = esriFieldType.esriFieldTypeDouble;
            pFieldsEdit.AddField(pField);

            pField = new FieldClass();
            pFieldEdit = (IFieldEdit)pField;
            pFieldEdit.Length_2 = 10;
            pFieldEdit.Name_2 = "BM";
            pFieldEdit.AliasName_2 = "编码";
            pFieldEdit.Type_2 = esriFieldType.esriFieldTypeString;
            pFieldsEdit.AddField(pField);

            pField = new FieldClass();
            pFieldEdit = (IFieldEdit)pField;
            pFieldEdit.Length_2 = 10;
            pFieldEdit.Name_2 = "GDDM";
            pFieldEdit.AliasName_2 = "管点代码";
            pFieldEdit.Type_2 = esriFieldType.esriFieldTypeString;
            pFieldsEdit.AddField(pField);

            pField = new FieldClass();
            pFieldEdit = (IFieldEdit)pField;
            pFieldEdit.Length_2 = 10;
            pFieldEdit.Name_2 = "BZ";
            pFieldEdit.AliasName_2 = "备注";
            pFieldEdit.Type_2 = esriFieldType.esriFieldTypeString;
            pFieldsEdit.AddField(pField);
            #endregion

            if (data != null)
            {
                foreach (var layer in layersList)
                {
                    string name = layer + "点";
                    data.CreateFeatureClass(name, pFields, null, null, esriFeatureType.esriFTSimple, "Shape", null);
                    IFeatureClass fatureclass = GetFcFromDataset(data, "LSGXPT." + name);
                    IFeatureCursor fcr = fatureclass.Insert(true);
                    IFeatureBuffer pfb;

                    foreach (DataRow row in this._ptDt.Rows)
                    {
                        if ((string)row["GXZL"] == layer)
                        {
                            IPoint pt = ConStructPoint(row);
                            AddPointToGraphicsLayer(pt);
                            pfb = fatureclass.CreateFeatureBuffer();

                            #region 设置属性信息
                            pfb.set_Value(fatureclass.Fields.FindField("GXZL"), row["GXZL"]);
                            pfb.set_Value(fatureclass.Fields.FindField("QSDW"), row["QSDW"]);
                            pfb.set_Value(fatureclass.Fields.FindField("GDMS"), row["GDMS"]);
                            pfb.set_Value(fatureclass.Fields.FindField("TZD"), row["TZD"]);
                            pfb.set_Value(fatureclass.Fields.FindField("FSW"), row["FSW"]);
                            //pfb.set_Value(fatureclass.Fields.FindField("GC"), row["GC"]);
                            pfb.set_Value(fatureclass.Fields.FindField("JSND"), row["JSND"]);
                            pfb.set_Value(fatureclass.Fields.FindField("DCRQ"), row["DCRQ"]);
                            pfb.set_Value(fatureclass.Fields.FindField("DCDW"), row["DCDW"]);
                            pfb.set_Value(fatureclass.Fields.FindField("SSDL"), row["SSDL"]);
                            pfb.set_Value(fatureclass.Fields.FindField("TFH"), row["TFH"]);
                            pfb.set_Value(fatureclass.Fields.FindField("WTDH"), row["WTDH"]);
                            pfb.set_Value(fatureclass.Fields.FindField("TSDH"), row["TSDH"]);
                            pfb.set_Value(fatureclass.Fields.FindField("XZJ"), row["XZJ"]);
                            pfb.set_Value(fatureclass.Fields.FindField("BM"), row["BM"]);
                            pfb.set_Value(fatureclass.Fields.FindField("GDDM"), row["GDDM"]);
                            //pfb.set_Value(fatureclass.Fields.FindField("BZ"), row["BZ"]);
                            #endregion

                            pfb.Shape = pt as IGeometry;
                            fcr.InsertFeature(pfb);
                        }
                    }
                    fcr.Flush();

                    IFeatureLayer fl = new FeatureLayer();
                    fl.FeatureClass = fatureclass;
                    fl.Name = name;
                    Extern.axMapControl地图预览.Map.AddLayer(fl as ILayer);
                    Extern.axMapControl地图预览.Refresh();
                }
            }
            
        }

        /// <summary>
        /// 绘制管线
        /// </summary>
        private void DrawLine()
        {
            List<string> layersList = new List<string>();
            foreach (DataRow i in this._lineDt.Rows)
                if (!layersList.Contains((string)i["GXZL"])) layersList.Add((string)i["GXZL"]);
            IWorkspace pWSF = FindWsByDefault();
            IEnumDataset dataSet = pWSF.get_Datasets(esriDatasetType.esriDTFeatureDataset);
            IFeatureDataset data = dataSet.Next() as IFeatureDataset;
            while (data != null)
            {
                if (data.Name == "LSGXPT.WorkSpaceForLSPipe")
                    break;
                else data = dataSet.Next() as IFeatureDataset;
            }

            #region//属性编辑
            IFields pFields = new FieldsClass();
            IFieldsEdit pFieldsEdit = (IFieldsEdit)pFields;

            IField pField = new FieldClass();
            IFieldEdit pFieldEdit = (IFieldEdit)pField;
            pFieldEdit.Name_2 = "Shape";
            pFieldEdit.Type_2 = esriFieldType.esriFieldTypeGeometry;
            IGeometryDef pGeometryDef = new GeometryDefClass();
            IGeometryDefEdit pGDefEdit = (IGeometryDefEdit)pGeometryDef;
            pGDefEdit.GeometryType_2 = esriGeometryType.esriGeometryPolyline;

            pFieldEdit.GeometryDef_2 = pGeometryDef;
            pGDefEdit.SpatialReference_2 = LoadProjectedCoordinateSystem();
            pFieldsEdit.AddField(pField);

            pField = new FieldClass();
            pFieldEdit = (IFieldEdit)pField;
            pFieldEdit.Length_2 = 20;
            pFieldEdit.Name_2 = "GXZL";
            pFieldEdit.AliasName_2 = "管线种类";
            pFieldEdit.Type_2 = esriFieldType.esriFieldTypeString;
            pFieldsEdit.AddField(pField);

            pField = new FieldClass();
            pFieldEdit = (IFieldEdit)pField;
            pFieldEdit.Length_2 = 20;
            pFieldEdit.Name_2 = "QSDW";
            pFieldEdit.AliasName_2 = "权属单位";
            pFieldEdit.Type_2 = esriFieldType.esriFieldTypeString;
            pFieldsEdit.AddField(pField);

            pField = new FieldClass();
            pFieldEdit = (IFieldEdit)pField;
            pFieldEdit.Length_2 = 20;
            pFieldEdit.Name_2 = "GXCD";
            pFieldEdit.AliasName_2 = "管线长度";
            pFieldEdit.Type_2 = esriFieldType.esriFieldTypeString;
            pFieldsEdit.AddField(pField);

            pField = new FieldClass();
            pFieldEdit = (IFieldEdit)pField;
            pFieldEdit.Length_2 = 20;
            pFieldEdit.Name_2 = "QDMS";
            pFieldEdit.AliasName_2 = "起点埋深";
            pFieldEdit.Type_2 = esriFieldType.esriFieldTypeDouble;
            pFieldsEdit.AddField(pField);

            pField = new FieldClass();
            pFieldEdit = (IFieldEdit)pField;
            pFieldEdit.Length_2 = 20;
            pFieldEdit.Name_2 = "ZDMS";
            pFieldEdit.AliasName_2 = "止点埋深";
            pFieldEdit.Type_2 = esriFieldType.esriFieldTypeDouble;
            pFieldsEdit.AddField(pField);

            pField = new FieldClass();
            pFieldEdit = (IFieldEdit)pField;
            pFieldEdit.Length_2 = 20;
            pFieldEdit.Name_2 = "GXCZ";
            pFieldEdit.AliasName_2 = "管线材质";
            pFieldEdit.Type_2 = esriFieldType.esriFieldTypeString;
            pFieldsEdit.AddField(pField);

            pField = new FieldClass();
            pFieldEdit = (IFieldEdit)pField;
            pFieldEdit.Length_2 = 20;
            pFieldEdit.Name_2 = "MSFS";
            pFieldEdit.AliasName_2 = "埋设方式";
            pFieldEdit.Type_2 = esriFieldType.esriFieldTypeString;
            pFieldsEdit.AddField(pField);

            pField = new FieldClass();
            pFieldEdit = (IFieldEdit)pField;
            pFieldEdit.Length_2 = 20;
            pFieldEdit.Name_2 = "GJ";
            pFieldEdit.AliasName_2 = "管径";
            pFieldEdit.Type_2 = esriFieldType.esriFieldTypeDouble;
            pFieldsEdit.AddField(pField);

            pField = new FieldClass();
            pFieldEdit = (IFieldEdit)pField;
            pFieldEdit.Length_2 = 20;
            pFieldEdit.Name_2 = "DMCC";
            pFieldEdit.AliasName_2 = "断面尺寸";
            pFieldEdit.Type_2 = esriFieldType.esriFieldTypeString;
            pFieldsEdit.AddField(pField);

            pField = new FieldClass();
            pFieldEdit = (IFieldEdit)pField;
            pFieldEdit.Length_2 = 20;
            pFieldEdit.Name_2 = "ZKS";
            pFieldEdit.AliasName_2 = "总孔数";
            pFieldEdit.Type_2 = esriFieldType.esriFieldTypeInteger;
            pFieldsEdit.AddField(pField);

            pField = new FieldClass();
            pFieldEdit = (IFieldEdit)pField;
            pFieldEdit.Length_2 = 20;
            pFieldEdit.Name_2 = "YYKS";
            pFieldEdit.AliasName_2 = "已用孔数";
            pFieldEdit.Type_2 = esriFieldType.esriFieldTypeInteger;
            pFieldsEdit.AddField(pField);

            pField = new FieldClass();
            pFieldEdit = (IFieldEdit)pField;
            pFieldEdit.Length_2 = 20;
            pFieldEdit.Name_2 = "JSND";
            pFieldEdit.AliasName_2 = "建设年代";
            pFieldEdit.Type_2 = esriFieldType.esriFieldTypeDate;
            pFieldsEdit.AddField(pField);

            pField = new FieldClass();
            pFieldEdit = (IFieldEdit)pField;
            pFieldEdit.Length_2 = 20;
            pFieldEdit.Name_2 = "QDGC";
            pFieldEdit.AliasName_2 = "起点高程";
            pFieldEdit.Type_2 = esriFieldType.esriFieldTypeString;
            pFieldsEdit.AddField(pField);

            pField = new FieldClass();
            pFieldEdit = (IFieldEdit)pField;
            pFieldEdit.Length_2 = 20;
            pFieldEdit.Name_2 = "ZDGC";
            pFieldEdit.AliasName_2 = "止点高程";
            pFieldEdit.Type_2 = esriFieldType.esriFieldTypeString;
            pFieldsEdit.AddField(pField);

            pField = new FieldClass();
            pFieldEdit = (IFieldEdit)pField;
            pFieldEdit.Length_2 = 20;
            pFieldEdit.Name_2 = "GS";
            pFieldEdit.AliasName_2 = "根数";
            pFieldEdit.Type_2 = esriFieldType.esriFieldTypeInteger;
            pFieldsEdit.AddField(pField);

            pField = new FieldClass();
            pFieldEdit = (IFieldEdit)pField;
            pFieldEdit.Length_2 = 20;
            pFieldEdit.Name_2 = "YQ";
            pFieldEdit.AliasName_2 = "压强";
            pFieldEdit.Type_2 = esriFieldType.esriFieldTypeString;
            pFieldsEdit.AddField(pField);

            pField = new FieldClass();
            pFieldEdit = (IFieldEdit)pField;
            pFieldEdit.Length_2 = 20;
            pFieldEdit.Name_2 = "DY";
            pFieldEdit.AliasName_2 = "电压";
            pFieldEdit.Type_2 = esriFieldType.esriFieldTypeString;
            pFieldsEdit.AddField(pField);

            pField = new FieldClass();
            pFieldEdit = (IFieldEdit)pField;
            pFieldEdit.Length_2 = 20;
            pFieldEdit.Name_2 = "QSDH";
            pFieldEdit.AliasName_2 = "起始点号";
            pFieldEdit.Type_2 = esriFieldType.esriFieldTypeString;
            pFieldsEdit.AddField(pField);

            pField = new FieldClass();
            pFieldEdit = (IFieldEdit)pField;
            pFieldEdit.Length_2 = 20;
            pFieldEdit.Name_2 = "ZZDH";
            pFieldEdit.AliasName_2 = "终止点号";
            pFieldEdit.Type_2 = esriFieldType.esriFieldTypeString;
            pFieldsEdit.AddField(pField);

            pField = new FieldClass();
            pFieldEdit = (IFieldEdit)pField;
            pFieldEdit.Length_2 = 20;
            pFieldEdit.Name_2 = "DCRQ";
            pFieldEdit.AliasName_2 = "调查日期";
            pFieldEdit.Type_2 = esriFieldType.esriFieldTypeDate;
            pFieldsEdit.AddField(pField);

            pField = new FieldClass();
            pFieldEdit = (IFieldEdit)pField;
            pFieldEdit.Length_2 = 20;
            pFieldEdit.Name_2 = "DCDW";
            pFieldEdit.AliasName_2 = "调查单位";
            pFieldEdit.Type_2 = esriFieldType.esriFieldTypeString;
            pFieldsEdit.AddField(pField);

            pField = new FieldClass();
            pFieldEdit = (IFieldEdit)pField;
            pFieldEdit.Length_2 = 20;
            pFieldEdit.Name_2 = "SSDL";
            pFieldEdit.AliasName_2 = "所属道路";
            pFieldEdit.Type_2 = esriFieldType.esriFieldTypeString;
            pFieldsEdit.AddField(pField);

            pField = new FieldClass();
            pFieldEdit = (IFieldEdit)pField;
            pFieldEdit.Length_2 = 20;
            pFieldEdit.Name_2 = "TFH";
            pFieldEdit.AliasName_2 = "图幅号";
            pFieldEdit.Type_2 = esriFieldType.esriFieldTypeString;
            pFieldsEdit.AddField(pField);

            pField = new FieldClass();
            pFieldEdit = (IFieldEdit)pField;
            pFieldEdit.Length_2 = 20;
            pFieldEdit.Name_2 = "BM";
            pFieldEdit.AliasName_2 = "编码";
            pFieldEdit.Type_2 = esriFieldType.esriFieldTypeString;
            pFieldsEdit.AddField(pField);

            pField = new FieldClass();
            pFieldEdit = (IFieldEdit)pField;
            pFieldEdit.Length_2 = 20;
            pFieldEdit.Name_2 = "BZ";
            pFieldEdit.AliasName_2 = "备注";
            pFieldEdit.Type_2 = esriFieldType.esriFieldTypeString;
            pFieldsEdit.AddField(pField);
            #endregion

            if (data != null)
            {
                foreach (var layer in layersList)
                {
                    string name = layer + "线";
                    data.CreateFeatureClass(name, pFields, null, null, esriFeatureType.esriFTSimple, "Shape", null);
                    IFeatureClass fatureclass = GetFcFromDataset(data, "LSGXPT." + name);
                    IFeatureCursor fcr = fatureclass.Insert(true);
                    IFeatureBuffer pfb;

                    foreach (DataRow row in this._lineDt.Rows)
                    {
                        if ((string)row["GXZL"] == layer)
                        {
                            IPoint a = GetPointByID((string)row["QSDH"]);
                            IPoint b = GetPointByID((string)row["ZZDH"]);
                            // 创建Segment集合
                            object o = Type.Missing;
                            ISegmentCollection segments = new PathClass();

                            // 创建线段
                            ILine line = new LineClass();
                            line.PutCoords(a, b);
                            segments.AddSegment(line as ISegment, ref o, ref o);

                            //创建一个Path对象
                            IPath path = segments as IPath;

                            IGeometryCollection ployline = new PolylineClass();
                            ployline.AddGeometry(path as IGeometry, ref o, ref o);

                            pfb = fatureclass.CreateFeatureBuffer();

                            #region 设置属性信息
                            pfb.set_Value(fatureclass.Fields.FindField("GXZL"), row["GXZL"]);
                            pfb.set_Value(fatureclass.Fields.FindField("QSDW"), row["QSDW"]);
                            pfb.set_Value(fatureclass.Fields.FindField("GXCD"), row["GXCD"]);
                            pfb.set_Value(fatureclass.Fields.FindField("QDMS"), row["QDMS"]);
                            pfb.set_Value(fatureclass.Fields.FindField("ZDMS"), row["ZDMS"]);
                            pfb.set_Value(fatureclass.Fields.FindField("GXCZ"), row["GXCZ"]);
                            pfb.set_Value(fatureclass.Fields.FindField("MSFS"), row["MSFS"]);
                            pfb.set_Value(fatureclass.Fields.FindField("GJ"), row["GJ"]);
                            pfb.set_Value(fatureclass.Fields.FindField("DMCC"), row["DMCC"]);
                            pfb.set_Value(fatureclass.Fields.FindField("ZKS"), row["ZKS"]);
                            pfb.set_Value(fatureclass.Fields.FindField("YYKS"), row["YYKS"]);
                            pfb.set_Value(fatureclass.Fields.FindField("JSND"), row["JSND"]);
                            pfb.set_Value(fatureclass.Fields.FindField("QDGC"), row["QDGC"]);
                            pfb.set_Value(fatureclass.Fields.FindField("ZDGC"), row["ZDGC"]);
                            pfb.set_Value(fatureclass.Fields.FindField("GS"), row["GS"]);
                            pfb.set_Value(fatureclass.Fields.FindField("YQ"), row["YQ"]);
                            pfb.set_Value(fatureclass.Fields.FindField("DY"), row["DY"]);
                            pfb.set_Value(fatureclass.Fields.FindField("QSDH"), row["QSDH"]);
                            pfb.set_Value(fatureclass.Fields.FindField("ZZDH"), row["ZZDH"]);
                            pfb.set_Value(fatureclass.Fields.FindField("DCRQ"), row["DCRQ"]);
                            pfb.set_Value(fatureclass.Fields.FindField("DCDW"), row["DCDW"]);
                            pfb.set_Value(fatureclass.Fields.FindField("SSDL"), row["SSDL"]);
                            pfb.set_Value(fatureclass.Fields.FindField("TFH"), row["TFH"]);
                            pfb.set_Value(fatureclass.Fields.FindField("BM"), row["BM"]);
                            //pfb.set_Value(fatureclass.Fields.FindField("BZ"), row["BZ"]);
                            #endregion
                            AddLineToGraphicsLayer(ployline as IPolyline);
                            pfb.Shape = ployline as IGeometry;
                            fcr.InsertFeature(pfb);
                        }
                    }
                    fcr.Flush();
                    IFeatureLayer fl = new FeatureLayer();
                    fl.FeatureClass = fatureclass;
                    fl.Name = name;
                    Extern.axMapControl地图预览.Map.AddLayer(fl as ILayer);
                    Extern.axMapControl地图预览.Refresh();
                }
            }
        }

        /// <summary>
        /// 清空工作库
        /// </summary>
        private void ClearWorckSpace()
        {
            IWorkspace pWSF = FindWsByDefault();
            IEnumDataset dataSet = pWSF.get_Datasets(esriDatasetType.esriDTFeatureDataset);
            IFeatureDataset data = dataSet.Next() as IFeatureDataset;
            while (data != null)
            {
                if (data.Name == "LSGXPT.WorkSpaceForLSPipe")
                    break;
                else data = dataSet.Next() as IFeatureDataset;
            }
            if (data != null)
            {
                IFeatureClassContainer fcContainer = data as IFeatureClassContainer;
                int count = fcContainer.ClassCount;
                for (int i = 0; i < count; i++)
                {
                    IDataset ds = fcContainer.get_Class(0) as IDataset;
                    ds.Delete();
                }
            }
        }
        
        /// <summary>
        /// 打开工作库
        /// </summary>
        /// <returns></returns>
        private IWorkspace FindWsByDefault()
        {
            IPropertySet propSet = new PropertySetClass();
            propSet.SetProperty("Server", "132.1.10.236");
            propSet.SetProperty("Instance", "esri_sde");
            propSet.SetProperty("Database", "sde");
            propSet.SetProperty("User", "lsgxpt");
            propSet.SetProperty("Password", "lsgxpt");
            propSet.SetProperty("Version", "SDE.DEFAULT");
            IWorkspaceFactory factory = new SdeWorkspaceFactoryClass();
            IWorkspace workspace = factory.Open(propSet, 0);
            return workspace;
        }

        /// <summary>
        /// 获取空间参考
        /// </summary>
        /// <returns></returns>
        private IProjectedCoordinateSystem LoadProjectedCoordinateSystem()
        {
            ISpatialReferenceFactory spatialReferenceFactory = new SpatialReferenceEnvironmentClass();
            IProjectedCoordinateSystem projectedCoordinateSystem =
                spatialReferenceFactory.CreateESRISpatialReferenceFromPRJFile
                (@"C:\Program Files\ArcGIS\Desktop10.0\Coordinate Systems\Projected Coordinate Systems\Gauss Kruger\Xian 1980\Xian 1980 3 Degree GK CM 120E.prj") as IProjectedCoordinateSystem;
            return projectedCoordinateSystem;
        }

        /// <summary>
        /// 根据管点WTDH获取并生成管点的IPoint
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        private IPoint GetPointByID(string id)
        {
            DataRow[] checkedResultRow = this._ptDt.Select(string.Format("WTDH='{0}'", id));
            if (checkedResultRow.Length > 0)
                return ConStructPoint(checkedResultRow[0]);
            return null;
        }

        /// <summary>
        /// 将管点表数据行生成IPoint类型
        /// </summary>
        /// <param name="dr"></param>
        /// <returns></returns>
        private IPoint ConStructPoint(DataRow dr)
        {
            IPoint pt = new Point()
            {
                X = Convert.ToDouble(dr["X"]),
                Y = Convert.ToDouble(dr["Y"])
            };
            return pt;
        }

        /// <summary>
        /// 根据名称从数据集中获取FeaturClass
        /// </summary>
        /// <param name="featDs"></param>
        /// <param name="className"></param>
        /// <returns></returns>
        private IFeatureClass GetFcFromDataset(IFeatureDataset featDs, string className)
        {
            IFeatureClass featClass;
            IFeatureClassContainer fcContainer = featDs as IFeatureClassContainer;
            for (int i = 0; i < fcContainer.ClassCount; i++)
            {
                featClass = fcContainer.get_Class(i);
                if (featClass.AliasName == className)
                {
                    return featClass;
                }
            }
            return null;
        }

        /// </summary>
        /// <param name="subgraphicsLayername">图层名称唯一</param>
        /// <returns></returns>
        public IGraphicsLayer AddSubGraphicsLayer(IMap map, string subgraphicsLayername)
        {
            ICompositeGraphicsLayer pCompositeGLayer = map.BasicGraphicsLayer as ICompositeGraphicsLayer;
            IGraphicsLayer pGLayer = null;
            try
            {
                //查找是否已存在,如果不存在,跳转到catch内容.
                //如果查到不到,说明集合中并没有指定名称的graphicslayer
                pGLayer = pCompositeGLayer.FindLayer(subgraphicsLayername);
            }
            catch
            {
                //若不存在,则添加一个指定名称的GraphicsLayer
                pGLayer = pCompositeGLayer.AddLayer(subgraphicsLayername, null);
            }
            return pGLayer;
        }

        private void AddLineToGraphicsLayer(IPolyline line)
        {
            ISimpleLineSymbol pLineSymbol = new SimpleLineSymbolClass();  //设置Symbol属性
            //pLineSymbol.Color = GetRGB(0, 255, 0);
            pLineSymbol.Width = 2;

            ILineElement pLineElement = new LineElementClass();
            IElement pEle = pLineElement as IElement;
            pLineElement.Symbol = pLineSymbol;
            pEle.Geometry = line;

            gc.AddElement(pEle, 0);
            Extern.axMapControl地图预览.ActiveView.Extent = line.Envelope;
            Extern.axMapControl地图预览.Refresh();
            //Extern.map.ActiveView
            //Extern.map.ActiveView.PartialRefresh(esriViewDrawPhase.esriViewGraphics, null, null);
        }

        private void AddPointToGraphicsLayer(IPoint pt)
        {
            ISimpleMarkerSymbol ptSymbol = new SimpleMarkerSymbolClass();  //设置Symbol属性
            //pLineSymbol.Color = GetRGB(0, 255, 0);
            ptSymbol.Size = 5;

            IMarkerElement pMarkerElement = new MarkerElementClass();
            IElement pEle = pMarkerElement as IElement;
            pMarkerElement.Symbol = ptSymbol;
            pEle.Geometry = pt;

            gc.AddElement(pEle, 0);
            
            Extern.axMapControl地图预览.ActiveView.Extent = pt.Envelope;
            //Extern.map.CenterAt(pt);
            Extern.axMapControl地图预览.Refresh();
            //Extern.map.ActiveView.PartialRefresh(esriViewDrawPhase.esriViewGraphics, null, null);
        }
    }
}
